package cn.dglydrpy.study.j2ee.mq.rocketmq.produer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class RocketProducer {

    public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException, RemotingException, MQClientException, MQBrokerException {
        
    	// 在实例化生产者的同时，指定了生产组名称
        DefaultMQProducer producer = new DefaultMQProducer("rocket_pgrp_01");

        // 指定NameServer的地址
        producer.setNamesrvAddr("192.168.80.131:9876");

        // 对生产者进行初始化，然后就可以使用了
        producer.start();

        // 创建消息，第一个参数是主题名称，第二个参数是消息内容
        List<Message> messages = new ArrayList<>();
        
        for(int i = 0;i < 100; i++) {
        	 Message message = new Message(
                     "rocket_topic_01",
                     ("hello" + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
             );
        	 messages.add(message);
        }
       
        // 发送消息
        final SendResult result = producer.send(messages);
        System.out.println(result);

        // 关闭生产者
        producer.shutdown();
    }


}
